import React from 'react'

export default class React16 extends React.Component{
    render(){
        return (
            <A/>
        )
    }
}


class A extends React.Component{
    state={
        a:1,
    }
    click=()=>{
        this.setState({a:2})
    }
    render(){
        const {a}=this.state
        return (
            <div>
                <div onClick={this.click}>A:{a}</div>
                <B a={a} />
            </div>
        )
    }
}

class B extends React.Component{
    state={
        b:1,
        a:'null',
    }
    // UNSAFE_componentWillMount(){
    //     const {a}=this.props
    //     console.log(a)
    // }
    //父组件传给子组件的state时使用
    static getDerivedStateFromProps(np,ns){
        if(np.a!=ns.a){
            return {
                a:np.a,
            }
        }
        return null
    }
    render(){
        const {b,a}=this.state
        return (
            <div>B:{b}-{a}</div>
        )
    }
    componentDidCatch(){
        console.log(111)
    }
}